#include "JVM_Class.h"
#include "JVM_CP_Table.h"
#include "JVM_StatusCodes.h"
#include "JVM_Handle.h"








/****************************************************************************************/
/*																						*/
/*	Function : JVM_Class_InitFields							Author : OG					*/
/*																						*/
/*	Description : 								   										*/
/*																						*/
/*	Version : 1.0					Date :2011.1.3			Status : Working   			*/
/*																						*/
/****************************************************************************************/

uint16_t JVM_Class_InitFields(JVM_Class_t *myClass)
{

	uint16_t fieldCounter;
	uint8_t fieldType;
	uint16_t dataOffset;

	uint16_t constantValueIndex;

	JVM_CP_Integer_t * tmpIntegerInfo;
	JVM_CP_Long_t * tmpLongInfo;
	JVM_CP_Float_t * tmpFloatInfo;
	JVM_CP_Double_t * tmpDoubleInfo;


	union data_value myValue;
	uint16_t handleNumber;

	for (fieldCounter=0;fieldCounter<JVM_Class_getFieldCount(myClass);fieldCounter++)
		{
			constantValueIndex=JVM_Class_getFieldConstantValueReference(myClass,fieldCounter);
			if (constantValueIndex!=0)
				{


				fieldType=JVM_Class_getFieldType(myClass,fieldCounter);
				dataOffset=JVM_Class_getFieldDataOffset(myClass,fieldCounter);
				handleNumber=myClass->staticDataHandle;


				switch(fieldType)
					{
					case FIELD_TYPE_BYTE:
					case FIELD_TYPE_BOOLEAN:
						tmpIntegerInfo=JVM_CPTable_getIntegerInfo(myClass->constantPoolTable,constantValueIndex);
						myValue.byteValue=tmpIntegerInfo->value;


					case FIELD_TYPE_CHAR:
						tmpIntegerInfo=JVM_CPTable_getIntegerInfo(myClass->constantPoolTable,constantValueIndex);
						myValue.charValue=tmpIntegerInfo->value;

					case FIELD_TYPE_SHORT:
						tmpIntegerInfo=JVM_CPTable_getIntegerInfo(myClass->constantPoolTable,constantValueIndex);
						myValue.shortValue=tmpIntegerInfo->value;

					case FIELD_TYPE_ARRAY_REFERENCE:
					case FIELD_TYPE_CLASS_REFERENCE:
						tmpIntegerInfo=JVM_CPTable_getIntegerInfo(myClass->constantPoolTable,constantValueIndex);
						myValue.reference=tmpIntegerInfo->value;

					case FIELD_TYPE_INT:
						tmpIntegerInfo=JVM_CPTable_getIntegerInfo(myClass->constantPoolTable,constantValueIndex);
						myValue.intValue=tmpIntegerInfo->value;
						break;


					case FIELD_TYPE_LONG:
						tmpLongInfo=JVM_CPTable_getLongInfo(myClass->constantPoolTable,constantValueIndex);
						myValue.longValue=tmpLongInfo->value;
						break;


					case FIELD_TYPE_FLOAT:
						tmpFloatInfo=JVM_CPTable_getFloatInfo(myClass->constantPoolTable,constantValueIndex);
						myValue.floatValue=tmpFloatInfo->value;
						break;


					case FIELD_TYPE_DOUBLE:
						tmpDoubleInfo=JVM_CPTable_getDoubleInfo(myClass->constantPoolTable,constantValueIndex);
						myValue.doubleValue=tmpDoubleInfo->value;
						break;

					default:
						//TODO Error !!!!
						break;
					}

				switch(fieldType)
					{
					case FIELD_TYPE_BYTE:
					case FIELD_TYPE_BOOLEAN:
						return(JVM_Handle_staticSetByte(handleNumber,dataOffset,myValue.byteValue));
						break;


					case FIELD_TYPE_CHAR:
					case FIELD_TYPE_SHORT:
						return(JVM_Handle_staticSetShort(handleNumber,dataOffset,myValue.shortValue));
						break;


					case FIELD_TYPE_INT:
						return(JVM_Handle_staticSetInt(handleNumber,dataOffset,myValue.intValue));
						break;

					case FIELD_TYPE_FLOAT:
						return(JVM_Handle_staticSetFloat(handleNumber,dataOffset,myValue.floatValue));
						break;


					case FIELD_TYPE_ARRAY_REFERENCE:
					case FIELD_TYPE_CLASS_REFERENCE:
						return(JVM_Handle_staticSetReference(handleNumber,dataOffset,myValue.reference));
						break;

					case FIELD_TYPE_LONG:
						return(JVM_Handle_staticSetLong(handleNumber,dataOffset,myValue.longValue));
						break;

					case FIELD_TYPE_DOUBLE:
						return(JVM_Handle_staticSetDouble(handleNumber,dataOffset,myValue.doubleValue));
						break;

					default:
						return(STCD_UNKNOWN_FIELD_TYPE);
					}


				}

		}

	return(STCD_OK);
}


